package com.glory.erp.invoice.goods.dao.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.glory.erp.invoice.goods.dao.GoodsDao;
import com.glory.erp.invoice.goods.bean.Goods;
import com.glory.erp.invoice.goods.bean.GoodsQuery;
import com.glory.erp.auth.base.BaseDaoImpl;
import com.glory.erp.auth.base.BaseQueryModel;

public class GoodsDaoImpl extends BaseDaoImpl<Goods> implements GoodsDao {

	public void doQbc(DetachedCriteria dc, BaseQueryModel qm) {
		GoodsQuery gqm = (GoodsQuery) qm;
		if (gqm.getUnit() != null && gqm.getUnit().trim().length() > 0) {
			dc.add(Restrictions.eq("unit", gqm.getUnit().trim()));
		}

		// gqm.gtm.sm.uuid
		if (gqm.getGoodsType() != null
				&& gqm.getGoodsType().getSupplier() != null
				&& gqm.getGoodsType().getSupplier().getUuid() != null
				&& gqm.getGoodsType().getSupplier().getUuid() != -1) {
			dc.createAlias("goodsType", "g");
			dc.createAlias("g.supplier", "s");
			dc.add(Restrictions.eq("s.uuid", gqm.getGoodsType().getSupplier()
					.getUuid()));
			/*
			 * dc.createAlias("goodsType", "g"); // goodsType
			 * dc.add(Restrictions.eq("g.supplier",
			 * gqm.getGoodsType().getSupplier()));
			 */
		}
		if (gqm.getName() != null && gqm.getName().trim().length() > 0) {
			dc.add(Restrictions.eq("name", gqm.getName()));
		}
		if (gqm.getProducer() != null && gqm.getProducer().trim().length() > 0) {
			dc.add(Restrictions.eq("producer", gqm.getProducer()));
		}
		System.out.println(gqm.getInPriceMin());
		System.out.println(gqm.getInPriceMax());
		if (gqm.getInPriceMin() != null && !gqm.getInPriceMin().equals("")
				&& gqm.getInPriceMax() != null
				&& !gqm.getInPriceMax().equals("")) {
			dc.add(Restrictions.ge("inPrice", Double.parseDouble(gqm.getInPriceMin())));
			dc.add(Restrictions.le("inPrice", Double.parseDouble(gqm.getInPriceMax())));
		}
		if (gqm.getOutPriceMin() != null && !gqm.getOutPriceMin().equals("")
				&& gqm.getOutPriceMax() != null
				&& !gqm.getOutPriceMin().equals("")) {
			dc.add(Restrictions.ge("outPrice", Double.parseDouble(gqm.getOutPriceMin())));
			dc.add(Restrictions.le("outPrice", Double.parseDouble(gqm.getOutPriceMax())));
		}
	}

	public List<Goods> findAllByGoodsType(Long uuid) {
		String hql = "from Goods where goodsType.uuid = ?";
		return this.getHibernateTemplate().find(hql, uuid);
	}

	public void goodsUseNumUpdate() {
		String hql = "update Goods g " + "set g.useNum = "
				+ "(select count(od.uuid) " + "from OrderDetail od "
				+ "where g.uuid = od.goods.uuid)";
		this.getHibernateTemplate().bulkUpdate(hql);
	}

	public List<Object[]> findWarnInfo() {
		/*
		 * String hql = "select g.name," + "sum(sd.num)>g.maxNum," +
		 * "sum(sd.num)<g.minNum " + "from Goods g,StoreDetail sd" +
		 * "where sd.goods.uuid = g.uuid" + "group by sd.goods.uuid"; // String
		 * hql =
		 * "SELECT g.name,SUM(sd.num)>g.maxNum,SUM(sd.num)<g.minNum FROM Goods g,StoreDetail sd WHERE sd.goods.uuid = g.uuid GROUP BY sd.goods.uuid"
		 * ; return this.getHibernateTemplate().find(hql);
		 */
		String sql = "SELECT g.name," + "SUM(sd.num)>g.maxNum,"
				+ "SUM(sd.num)<g.minNum " + "FROM goods g,storedetail sd "
				+ "WHERE sd.goodsuuid = g.uuid " + "GROUP BY sd.goodsuuid";
		SessionFactory sessionFactory = this.getHibernateTemplate()
				.getSessionFactory();
		Session currentSession = sessionFactory.getCurrentSession();
		SQLQuery sq = currentSession.createSQLQuery(sql);
		return sq.list();
	}

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml", "applicationContext-goods.xml");
		GoodsDao dao = (GoodsDao) ctx.getBean("goodsDao");
		System.out.println(dao.findWarnInfo());
	}
}